package org.shj.netty.chartper7;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqClientHandler extends ChannelHandlerAdapter{
	
	public SubReqClientHandler(){
		
	}
	
	public void channelActive(ChannelHandlerContext ctx){
		for(int i = 0 ; i < 10; i++){
			ctx.write(subReq(i));
		}
		ctx.flush();
	}
	
	private SubscribeReq subReq(int i){
		SubscribeReq req = new SubscribeReq();
		req.setAddress("南京市江宁区");
		req.setPhone("13812345678");
		req.setSubReqId(i);
		req.setProductName("Netty 权威指南");
		req.setUserName("SHJ");
		return req;
	}
	
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
		System.out.println("Reveive server response: "+ msg);
	}
	
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
		System.out.println("channelReadComplete()");
		ctx.flush();
	}
	
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
		cause.printStackTrace();
		ctx.close();
	}
}
